<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('addresses', function (Blueprint $table) {
      $table->id();
      $table->bigInteger('user_id')->unsigned();
      $table->bigInteger('district_id')->unsigned();
      $table->bigInteger('city_id')->unsigned();
      $table->bigInteger('zipcode_id')->unsigned();
      $table->bigInteger('type_id')->unsigned();
      $table->string('address');
      $table->string('house_number', 10);
      $table->string('hamlet')->nullable();
      $table->boolean('active')->default(true);
      $table->timestamps();
      // Foreign keys constraints
      $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');
      $table->foreign('district_id')->references('id')->on('districts')->onDelete('restrict');
      $table->foreign('city_id')->references('id')->on('cities')->onDelete('restrict');
      $table->foreign('zipcode_id')->references('id')->on('zipcodes')->onDelete('restrict');
      $table->foreign('type_id')->references('id')->on('types')->onDelete('restrict');
      // Unique candidate key constraints
      $table->unique(['user_id', 'type_id']);
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('addresses');
  }
};
